package com.yonyou.gmmc.service.pointactivity.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.pointactivity.constant.PointActivityConstant;
import com.yonyou.gmmc.service.pointactivity.dao.NotReturnFactoryMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TcSysConfigMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TmOwnerCarRelationMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TmPotentialUserMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TrUserOwnerMapper;
import com.yonyou.gmmc.service.pointactivity.util.DateUtils;

@Transactional
@Service("NotReturnFactoryService")
public class NotReturnFactoryService {

	private Logger logger = Logger.getLogger(NotReturnFactoryService.class);

	@Autowired
	private TmPotentialUserMapper tmPotentialUserMapper;

	@Autowired
	private TrUserOwnerMapper trUserOwnerMapper;

	@Autowired
	private TmOwnerCarRelationMapper tmOwnerCarRelationMapper;

	@Autowired
	private NotReturnFactoryMapper notReturnFactoryMapper;

	@Autowired
	private TcSysConfigMapper tcSysConfigMapper;

	public void recordVisit(Map<String, Object> param) {
		notReturnFactoryMapper.recordVisit(param);
	}

	/**
	 * 判断当前用户所在环节以及权限
	 * @param openId
	 * @return
	 */
	public Map<String, Object> checkUserProgress(String openId) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		// ------------
		Map<String,Object> param = new HashMap<String,Object>();
		
		param.put("openId", openId);
		param.put("qneTitle", PointActivityConstant.QNE_TITLE);
		// ------------
		
		List<Map<String, Object>> keyList = tcSysConfigMapper.viewTcSysConfigByKey(getKeyList());

		Date startDate = DateUtils.stringToDate("2017-09-01 00:00:00");
		Date endDate = DateUtils.stringToDate("2017-09-25 23:59:59");
		Date currentDate = DateUtils.getCurrentDateTime();

		for (Map<String, Object> map : keyList) {
			if (PointActivityConstant.SYS_CONFIG_KEY_07.equals(map.get("SYS_CONFIG_KEY").toString())) {
				startDate = DateUtils.stringToDate1(map.get("SYS_CONFIG_VALUE").toString());
			} else if (PointActivityConstant.SYS_CONFIG_KEY_08.equals(map.get("SYS_CONFIG_KEY").toString())) {
				endDate = DateUtils.stringToDate1(map.get("SYS_CONFIG_VALUE").toString());
			}
		}

		if (currentDate.getTime() < startDate.getTime()) {
			resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_01);
		} else if (currentDate.getTime() > endDate.getTime()) {
			resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_02);
		} else {
			// 只有车主可以参加活动
			if (judgeOpenIdType(openId).intValue() == PointActivityConstant.USER_TYPE_3.intValue()) {
				// 判断是否25万之中的.
				if(Boolean.valueOf(notReturnFactoryMapper.checkAccordVehicle(openId))){
					// 是否答题完
					if(Boolean.valueOf(notReturnFactoryMapper.isAnswer(param))){
						// 判断是否抽奖
						if(Boolean.valueOf(notReturnFactoryMapper.isExtract(openId))){
							// 是否中奖
							if(Boolean.valueOf(notReturnFactoryMapper.isWin(openId))){
								resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_05);
							} else {
								resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_06);
							}
						} else {
							resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_07);
						}
					} else {
						resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_04);
					}
				} else {
					resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_03);
				}
			} else {
				resultMap.put(PointActivityConstant.INIT_RESULT_TYPE, PointActivityConstant.NOT_RETURN_FACTORY_STATUS_00);
			}
		}
		return resultMap;
	}

	/**
	 * 查询TC_SYS_CONFIG表时,传入的条件
	 * 
	 * @return
	 */
	public List<String> getKeyList() {
		List<String> keyList = new ArrayList<String>();
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_07);
		keyList.add(PointActivityConstant.SYS_CONFIG_KEY_08);
		return keyList;
	}

	/**
	 * 根据openId 判断 是粉丝,会员,车主.
	 * 
	 * @param openId
	 * @return
	 */
	public Integer judgeOpenIdType(String openId) {

		Map<String, Object> potentialMap = queryPotentialUser(openId);

		Map<String, Object> memberMap = null != potentialMap ? queryMemberInfo(potentialMap.get("POTENTIAL_USER_ID").toString()) : null;

		Map<String, Object> vehcileMap = null != memberMap ? queryVehcile(memberMap.get("USER_ID").toString()) : null;

		Integer userType = returnTypeByOpenId(potentialMap, memberMap, vehcileMap);

		logger.info("去拉萨活动获取到的的openId为:" + openId + ",根据openId判断出用户类型是:" + userType);

		return userType;
	}

	/**
	 * 查询潜客
	 * 
	 * @param openId
	 * @return
	 */
	public Map<String, Object> queryPotentialUser(String openId) {
		return tmPotentialUserMapper.queryPotentialUserByOpenId(openId);
	}

	/**
	 * 查询会员
	 * 
	 * @param potentialUserId
	 * @return
	 */
	public Map<String, Object> queryMemberInfo(String potentialUserId) {
		return trUserOwnerMapper.queryMemberInfoByPotentialUserId(potentialUserId);
	}

	/**
	 * 查询车辆
	 * 
	 * @param userId
	 * @return
	 */
	public Map<String, Object> queryVehcile(String userId) {
		return tmOwnerCarRelationMapper.queryVehcileByMemberId(userId);
	}

	/**
	 * 根据三个map判断是车主还是粉丝还是会员
	 * 
	 * @param potentialMap
	 * @param memberMap
	 * @param vehcileMap
	 * @return
	 */
	public Integer returnTypeByOpenId(Map<String, Object> potentialMap, Map<String, Object> memberMap, Map<String, Object> vehcileMap) {

		return null != potentialMap ? null != memberMap ? null != vehcileMap ? PointActivityConstant.USER_TYPE_3 : PointActivityConstant.USER_TYPE_2 : PointActivityConstant.USER_TYPE_1 : PointActivityConstant.USER_TYPE_0;
	}

}
